package me.kuye.jkuye.upload;

import java.io.File;

import me.kuye.jkuye.helper.PathHelper;
import me.kuye.jkuye.util.StringUtil;

public class OreillyCos {

	public static void init(String saveDirectory, int maxPostSize, String encoding) {
		try {
			Class.forName("com.oreilly.servlet.MultipartRequest");
			doInit(saveDirectory, maxPostSize, encoding);
		} catch (ClassNotFoundException e) {

		}
	}

	private static void doInit(String saveDirectory, int maxPostSize, String encoding) {
		String dir;
		if (StringUtil.isEmpty(saveDirectory)) {
			dir = PathHelper.getWebRootPath() + File.separator + "upload";
		} else if (isAbsolutelyPath(saveDirectory)) {
			dir = saveDirectory;
		} else {
			dir = PathHelper.getWebRootPath() + File.separator + saveDirectory;
		}
		if (dir.endsWith("/") == false && dir.endsWith("\\") == false) {
			dir = dir + File.separator;
		}
		MultipartRequest.init(dir,maxPostSize,encoding);
	}

	private static boolean isAbsolutelyPath(String saveDirectory) {
		return saveDirectory.startsWith("/") || saveDirectory.indexOf(":") == 1;
	}
}
